package models

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
)

// DB https://gorm.io/zh_CN/docs/connecting_to_the_database.html

var Db *gorm.DB
var err error

// init mysql的连接
//
//	@Description: 初始化db
func init() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := "root:root123@tcp(127.0.0.1:3306)/gin?charset=utf8mb4&parseTime=True&loc=Local"
	Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		//SkipDefaultTransaction: true, //关闭事务
		QueryFields: true,
	})
	if err != nil {
		log.Println(err)
	}
	//设置日志级别
	Db.Logger = logger.Default.LogMode(logger.Info)
}
